﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;
using HRProject.Code.Common;

namespace HRProject.Code.DAO
{
    public class AgentDAO : BaseDAO
    {
        public AgentDAO()
            : base ()
        {
        }

        public List<BLAgent> Search(BLAgent filter, int totalPage, int pageSize, int pageIndex)
        {
            int startIndex = (pageIndex - 1) * pageSize + 1;
            if (pageIndex > totalPage)
            {
                startIndex = (totalPage - 1) * pageSize + 1;
            }
            else if (pageIndex < 1)
            {
                startIndex = 1;
            }
            int endIndex = startIndex + pageSize - 1;

            List<BLAgent> result = new List<BLAgent>();
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@id", filter.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@fullName", filter.FullName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@code", filter.Code, ParameterDirection.Input));
            condition.Add(new BaseCondition("@identifiedSerial", filter.IdentifiedSerial, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startIndex", startIndex, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endIndex", endIndex, ParameterDirection.Input));
            DataTable data = select("usp_SearchAgents", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                BLAgent agent = new BLAgent();
                agent.Id = (Guid)data.Rows[i]["AgentId"];
                agent.Code = (string)data.Rows[i]["AgentCode"];
                agent.FullName = (string)data.Rows[i]["FullName"];
                agent.Gender = (int)data.Rows[i]["Gender"];
                agent.BirthDate = (DateTime)data.Rows[i]["BirthDate"];
                agent.BirthPlace = (string)data.Rows[i]["BirthPlace"];
                agent.IdentifiedSerial = (string)data.Rows[i]["IdentifiedSerial"];
                agent.MaritalStatus = (string)data.Rows[i]["MaritalStatus"];
                agent.People = (string)data.Rows[i]["People"];
                agent.Phone = (string)data.Rows[i]["Phone"];
                agent.Address = (string)data.Rows[i]["Address"];
                agent.Contact = (string)data.Rows[i]["Contact"];
                agent.ExtraPhone = (string)data.Rows[i]["ExtraPhone"];
                agent.ExtraAddress = (string)data.Rows[i]["ExtraAddress"];
                agent.ExtraContact = (string)data.Rows[i]["ExtraContact"];
                agent.Avatar = (string)data.Rows[i]["Avatar"];
                agent.Description = (string)data.Rows[i]["Description"];
                agent.UpdateUserId = (Guid)data.Rows[i]["UpdateUserId"];
                agent.UpdateDate = (DateTime)data.Rows[i]["UpdateDate"];
                agent.DataSignature = (long)data.Rows[i]["DataSignature"];
                result.Add(agent);
            }
            return result;
        }

        public int CountAgent(BLJobTitle jobTitleFilter, Guid agentId, string fullName, DateTime downDate, DateTime upDate)
        {
            int result = 0;
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@jobTitleId", jobTitleFilter.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@id", agentId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@fullName", fullName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@jobStartDownDate", downDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@jobStartUpDate", upDate, ParameterDirection.Input));
            DataTable data = select("usp_CountAgentsInJobTitle", condition);
            if (data.Rows.Count > 0)
            {
                result = (int)data.Rows[0]["Total"];
            }
            return result;
        }

        public int AddAgent(BLAgent agent, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@id", agent.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@code", agent.Code, ParameterDirection.Input));
            condition.Add(new BaseCondition("@fullName", agent.FullName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@gender", agent.Gender, ParameterDirection.Input));
            condition.Add(new BaseCondition("@birthDate", agent.BirthDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@birthPlace", agent.BirthPlace, ParameterDirection.Input));
            condition.Add(new BaseCondition("@identifiedSerial", agent.IdentifiedSerial, ParameterDirection.Input));
            condition.Add(new BaseCondition("@maritalStatus", agent.MaritalStatus, ParameterDirection.Input));
            condition.Add(new BaseCondition("@people", agent.People, ParameterDirection.Input));
            condition.Add(new BaseCondition("@phone", agent.Phone, ParameterDirection.Input));
            condition.Add(new BaseCondition("@address", agent.Address, ParameterDirection.Input));
            condition.Add(new BaseCondition("@contact", agent.Contact, ParameterDirection.Input));
            condition.Add(new BaseCondition("@extraPhone", agent.ExtraPhone, ParameterDirection.Input));
            condition.Add(new BaseCondition("@extraAddress", agent.ExtraAddress, ParameterDirection.Input));
            condition.Add(new BaseCondition("@extraContact", agent.ExtraContact, ParameterDirection.Input));
            condition.Add(new BaseCondition("@avatar", agent.Avatar, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", agent.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", agent.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_AddAgent", condition);
            errorCode = (int)condition[condition.Count-1].ParamValue;
            return result;
        }

        public int QuickAddAgent(BLAgent agent, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@id", agent.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@fullName", agent.FullName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@birthDate", agent.BirthDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@identifiedSerial", agent.IdentifiedSerial, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", agent.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_QuickAddAgent", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;
            return result;
        }

        public int UpdateAgent(BLAgent agent, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@id", agent.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@code", agent.Code, ParameterDirection.Input));
            condition.Add(new BaseCondition("@fullName", agent.FullName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@gender", agent.Gender, ParameterDirection.Input));
            condition.Add(new BaseCondition("@birthDate", agent.BirthDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@birthPlace", agent.BirthPlace, ParameterDirection.Input));
            condition.Add(new BaseCondition("@identifiedSerial", agent.IdentifiedSerial, ParameterDirection.Input));
            condition.Add(new BaseCondition("@maritalStatus", agent.MaritalStatus, ParameterDirection.Input));
            condition.Add(new BaseCondition("@people", agent.People, ParameterDirection.Input));
            condition.Add(new BaseCondition("@phone", agent.Phone, ParameterDirection.Input));
            condition.Add(new BaseCondition("@address", agent.Address, ParameterDirection.Input));
            condition.Add(new BaseCondition("@contact", agent.Contact, ParameterDirection.Input));
            condition.Add(new BaseCondition("@extraPhone", agent.ExtraPhone, ParameterDirection.Input));
            condition.Add(new BaseCondition("@extraAddress", agent.ExtraAddress, ParameterDirection.Input));
            condition.Add(new BaseCondition("@extraContact", agent.ExtraContact, ParameterDirection.Input));
            condition.Add(new BaseCondition("@avatar", agent.Avatar, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", agent.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", agent.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_UpdateAgent", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;
            return result;
        }

        public int QuickUpdateAgent(BLAgent agent, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@id", agent.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@fullName", agent.FullName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@birthDate", agent.BirthDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@identifiedSerial", agent.IdentifiedSerial, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", agent.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_QuickUpdateAgent", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;
            return result;
        }

        public int DeleteAgent(BLAgent agent, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@id", agent.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", agent.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_DeleteAgent", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;
            return result;
        }
    }
}
